package cn.yeyiu.netonlinevideo.viewmodel;

import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.InfoData;
import cn.yeyiu.netonlinevideo.bean.PageData;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.util.bean.MediaData;

public class DataListViewModel extends ViewModel {

    //                 不同源       不同类型        的         page
    private final Map<String, Map<String, PageData>> pageDatas = new HashMap<>();
    //                 不同源    的  type list
    private final Map<String, List<TypeData>> typeDatas = new HashMap<>();
    //        源（平台）    类型（分类） 分页（index） 数据list
    public Map<String, Map<String, Map<Integer, List<MediaData>>>> sourceDatas = new HashMap<>();

    /*
    {
        "code1": {
            "types": [],  typeDatas

            "type1": {
                "pages": {  pageDatas
                    "curPage": 0,
                    "maxPage": 99,
                }

                "0": []  sourceDatas
                "1": []
            },
        },
    }
     */

    // ----------------------------------------------
    private Map<String, Map<Integer, List<MediaData>>> getCodeData(String code) {
        if (!sourceDatas.containsKey(code)) {
            sourceDatas.put(code, new HashMap<>());
        }
        return sourceDatas.get(code);
    }

    private Map<Integer, List<MediaData>> getTypeData(String code, String type) {
        Map<String, Map<Integer, List<MediaData>>> codeData = getCodeData(code);
        if (!codeData.containsKey(type)) {
            codeData.put(type, new HashMap<>());
        }
        return codeData.get(type);
    }

    public List<MediaData> getIndexData(String code, String type, int index) {
        Map<Integer, List<MediaData>> typeData = getTypeData(code, type);
        if (!typeData.containsKey(index)) {
            typeData.put(index, new ArrayList<>());
        }
        return typeData.get(index);
    }

    public void updateMediaData(String code, String type, int index, List<MediaData> dataList) {
        Map<Integer, List<MediaData>> typeData = getTypeData(code, type);
        typeData.put(index, dataList);
    }

    public void updateMediaData(InfoData infoData) {
        updateMediaData(infoData.code, infoData.type,infoData.index, infoData.dataList);
    }

    // ----------------------------------------------
    // type list
    public List<TypeData> getTypeList(String code) {
        if (!typeDatas.containsKey(code)) {
            typeDatas.put(code, new ArrayList<>());
        }
        return typeDatas.get(code);
    }

    public void updateTypeList(String code, List<TypeData> typeList){
        typeDatas.put(code, typeList);
    }

    // ----------------------------------------------
    // page
    private Map<String, PageData> getTypePage(String code) {
        if (pageDatas.get(code) == null) {
            pageDatas.put(code, new HashMap<>());
        }
        return pageDatas.get(code);
    }

    public PageData getPage(String code, String type) {
        Map<String, PageData> a = getTypePage(code);
        if (a.get(type) == null) {
            a.put(type, new PageData(code, type));
        }
        return a.get(type);
    }


    public void updatePage(String code, String type, PageData pageData){
        getTypePage(code).put(type, pageData);
    }
    public void updatePage(InfoData infoData){
        updatePage(infoData.code, infoData.type,
                new PageData(infoData.code, infoData.type, infoData.index, infoData.maxIndex));
    }

    // ----------------------------------------------
    public void updateData(InfoData infoData) {
        // update media data
        updateMediaData(infoData);
        // update page data
        updatePage(infoData);
    }
}
